package org.lgiavedoni.android.franq.core.message;

import org.lgiavedoni.android.franq.core.contact.Contact;
import org.lgiavedoni.android.franq.core.question.Question;
import org.lgiavedoni.android.franq.core.speak.CommonPhraseHelper;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;

public abstract class MessageInterceptor {

	private Question question;

	protected MessageInterceptor() {
	}

	public abstract void processMessage(MessageIn message) throws Exception;

	public void fail() {
		CommunicationService.inst.sendMessage(CommonPhraseHelper.UNEXPECTED_ERROR_PHRASE);
	}

	public abstract String[] getInterceptorTags();

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	private boolean ask() {
		question.ask();
		if (question == null || question.isDiscarded()) {
			// CommunicationService.inst.sendMessage(CommonPhraseHelper.DISCARDED_QUESTION);
			return false;
		}
		return true;
	}

	public Contact makeContactQuestion(Question question) {
		setQuestion(question);
		return ask() ? (Contact) question.getResultValue() : null;
	}

	public String makeTextQuestion(Question question) {
		setQuestion(question);
		return ask() ? (String) question.getResultValue() : null;
	}

}
